package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.settings.AboutPageFragment;
import com.sec.android.app.myfiles.util.PreferenceUtils;

public class ConfirmDataUsageDialog extends DialogFragment {
    private static final String DIALOG_TAG = "DataUsage_dialog";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog alertDialog;
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        String appName = getString(R.string.app_name);
        String msg = getString(R.string.new_connect_alert);
        dialogBuilder.setTitle(R.string.use_network_connection);
        dialogBuilder.setMessage(String.format(msg, appName));
        dialogBuilder.setPositiveButton(R.string.ok, onOK());
        dialogBuilder.setNegativeButton(R.string.cancel, onCancel());

        dialogBuilder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_MENU)
                    return true;
                else
                    return false;
            }
        });
        alertDialog = dialogBuilder.create();
        alertDialog.setCanceledOnTouchOutside(false);

        return alertDialog;
    }

    private DialogInterface.OnClickListener onOK() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                PreferenceUtils.setDoNotShowDataUsagePopup(getActivity(), true);
                if (dialog != null) {
                    dialog.dismiss();
                    getTargetFragment().onActivityResult(R.string.use_network_connection, Activity.RESULT_OK, null);
                }
            }
        };
    }

    private DialogInterface.OnClickListener onCancel() {
        return new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                if (dialog != null) {
                    dialog.dismiss();
                    getTargetFragment().onActivityResult(R.string.use_network_connection, Activity.RESULT_CANCELED, null);
                }
            }
        };
    }

    public static void showDataUsageDialog(AboutPageFragment fragment) {
        if (fragment.getFragmentManager().findFragmentByTag(DIALOG_TAG) != null) {
            return;
        }
        ConfirmDataUsageDialog dataUsageDialog = new ConfirmDataUsageDialog();
        dataUsageDialog.setTargetFragment(fragment, R.string.use_network_connection);
        if (!dataUsageDialog.isAdded()) {
            dataUsageDialog.showAllowingStateLoss(fragment.getFragmentManager(), DIALOG_TAG);
        }
    }

    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }
}
